package com.sengled.cloud.biz.union.mgt.dao.sys;


import java.util.List;

import com.sengled.cloud.biz.union.mgt.dao.sys.provider.UserAccountDaoProvider;
import com.sengled.cloud.biz.union.mgt.dao.sys.provider.UserDaoProvider;
import com.sengled.cloud.biz.union.mgt.model.po.Role;
import com.sengled.cloud.biz.union.mgt.model.po.ShopAccount;
import com.sengled.cloud.biz.union.mgt.model.po.ShopUserAccountDetail;
import com.sengled.cloud.biz.union.mgt.model.po.User;
import com.sengled.cloud.biz.union.mgt.model.po.UserAccount;
import com.sengled.cloud.biz.union.mgt.model.po.UserAccountDetail;
import com.sengled.cloud.biz.union.mgt.model.po.UserAcctShopAcct;
import com.sengled.cloud.biz.union.mgt.model.po.UserRole;
import com.sengled.cloud.biz.union.mgt.model.request.SaveAccountPwdRequest;
import com.sengled.cloud.biz.union.mgt.model.request.SaveAccountStartStopRequest;
import com.sengled.cloud.biz.union.mgt.model.request.SaveUserAccountRequest;
import com.sengled.cloud.biz.union.mgt.model.request.SetAccountStatusRequest;
import com.sengled.cloud.biz.union.mgt.model.request.UserAccountListRequest;
import com.sengled.cloud.biz.union.mgt.model.request.UserListQueryRequest;
import com.sengled.cloud.biz.union.mgt.model.vo.SelectBoxDataVo;
import com.sengled.cloud.biz.union.mgt.model.vo.UserAccountsVo;
import com.sengled.cloud.biz.union.mgt.model.vo.UserDetailVo;
import com.sengled.cloud.biz.union.mgt.model.vo.UserVo;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface UserAccountDao {
    int deleteByPrimaryKey(Long id);

    int insert(UserAccount record);

    int insertSelective(UserAccount record);

    @Select("select id as id,account_name as accountName,account_pwd as accountPwd,user_id as userId,company_id as companyId,account_type as accountType,account_status as accountStatus," +
            "current_shop_id as currentShopId,int001 as int001 from user_account where seal_mark = 0 and id = #{id}")
    UserAccount selectByPrimaryKey(Long id);

    int updateByPrimaryKeySelective(UserAccount record);

    int updateByPrimaryKey(UserAccount record);

    @Select("select id as id,account_name as accountName,account_pwd accountPwd,user_id as userId,company_id as companyId,account_type as accountType,account_status as accountStatus,current_shop_id as currentShopId from user_account where seal_mark = 0 and enable_mark =1 and account_name = #{username} and account_status in(1,2)")
    UserAccount selectByUsername(String username);

    @Update(" update user_account set current_shop_id = #{shopId} where id = #{userId} ")
    int updateUserCurrentShopId(@Param("userId") Long userId,@Param("shopId") Long shopId);

    @Update(" update user_account set account_pwd = #{newPwd} where id = #{id}")
    int updateUserPwd(@Param("id") Long id,@Param("newPwd") String newPwd);
    
    @Update("UPDATE user_account SET seal_mark=1 WHERE id=#{id}")
    int deleteUserAccount(@Param("id") Long id);
   
    
    //company id  according to local login user
    @Insert("INSERT INTO user_account(account_name,account_pwd,user_id,account_type,account_status,company_id,operator_id,create_date,seal_mark) " +
            "VALUES(#{userAccount.accountName},#{userAccount.accountPwd},#{userAccount.userId},#{userAccount.accountType},#{userAccount.accountStatus},#{userAccount.companyId},#{userAccount.operatorId},#{userAccount.createDate},0)")
    @Options(useGeneratedKeys = true, keyProperty = "userAccount.id")
    int insertUserAccount(@Param("userAccount") UserAccount userAccount);

    
//    @Insert("INSERT INTO account_role(user_account_id,role_id,create_date,seal_mark) " +
//            "VALUES(#{userAccount.id},#{userAccount.roleId},#{userAccount.createDate},0)" )
//    @Options(useGeneratedKeys = true, keyProperty = "userAccount.id")
    @InsertProvider(type = UserAccountDaoProvider.class, method = "batchInsertAccountRole")
    int insertAccountRole(SaveUserAccountRequest userAccount);
    
    
//    @Insert("INSERT INTO user_acct_shop_acct(user_account_id,shop_account_id,create_date,seal_mark) " +
//            "VALUES(#{userAccount.id},#{userAccount.userId},#{userAccount.createDate},0)")
//    @Options(useGeneratedKeys = true, keyProperty = "userRole.id")
    @InsertProvider(type = UserAccountDaoProvider.class, method = "batchInsertAccountShop")
    int insertAccountShop(SaveUserAccountRequest userAccount);
    
    int insertSelective(User record);
	
    @Select("SELECT sa.id id,sa.account_name accountName,sa.account_pwd accountPwd,u.id userId,u.user_name userName,account_type accountType,CASE WHEN account_status=1 THEN '正式' WHEN account_status=2 THEN '测试' WHEN account_status=3 THEN '演示' ELSE '试用' END accountTypeDesc,"
    		+ "account_status accountStatus,CASE WHEN account_status=1 THEN '在线' WHEN account_status=2 THEN '离线' WHEN account_status=3 THEN '锁定' ELSE '失效' END  accountStatusDesc FROM user_account sa,user u WHERE sa.user_id=u.id and sa.id=#{id}")
    UserAccountDetail selectUserAccount(@Param("id") Long id);

    int updateByPrimaryKeySelective(User record);
    
    
    @Update("UPDATE user_account SET account_name=#{userAccount.accountName},user_id=#{userAccount.userId},account_type=#{userAccount.accountType},account_status=#{userAccount.accountStatus},company_id=#{userAccount.companyId},operator_id=#{userAccount.operatorId}"
    		+ ",modify_date=#{userAccount.modifyDate} WHERE id=#{userAccount.id}")
    int updateUserAccount(@Param("userAccount") UserAccount userAccount);
 
    
    @Update("UPDATE account_role SET user_account_id=#{userAccount.id},role_id=#{userAccount.roleId}},operator_id=#{userAccount.operatorId},"
    		+ "modify_date=#{userAccount.modifyDate} WHERE user_account_id=#{userAccount.id}")
    int updateAccountRole(@Param("userAccount") UserAccount userAccount);
    
    
    @Delete("delete from account_role WHERE user_account_id=#{id}")
    int deleteAccountRole(@Param("id") Long id);

    @Delete("delete from user_acct_shop_acct WHERE user_account_id=#{id}")
    int deleteUserShopAccount(@Param("id") Long id);
    
    @Select("SELECT id,shop_id shopId,shop_code shopCode,shop_name shopName from shop_account where int001 is null and company_id=#{companyId} and seal_mark=0 order by create_date desc")
    List<ShopAccount> getShopAccountList(@Param("companyId") Long companyId);
    
    @Select("SELECT id,shop_id shopId,shop_code shopCode,shop_name shopName from shop_account where int001 is null and company_id=#{companyId} and seal_mark=0 and id in(SELECT shop_account_id FROM user_acct_shop_acct WHERE user_account_id=#{accountId} and seal_mark=0) order by create_date desc")
    List<ShopAccount> getCurrentShopAccountList(@Param("companyId") Long companyId,@Param("accountId") Long accountId);
    
    @Select("SELECT sa.id from user_acct_shop_acct usa,shop_account sa where usa.user_account_id=#{id} and usa.shop_account_id=sa.id and sa.seal_mark=0")  
    List<Long> getSelectedShopAccountIds(@Param("id") Long id);
    
    
    @Select("SELECT sa.shop_id shopId,sa.shop_code shopCode,sa.shop_name shopName from user_acct_shop_acct usa,shop_account sa where usa.user_account_id=#{id} and usa.shop_account_id=sa.id and sa.seal_mark=0")  
    List<ShopUserAccountDetail> getShopUserAccountDetail(@Param("id") Long id);
//    @Insert("update user_role set role_id=#{userRole.roleId},modify_date=#{userRole.modifyDate}  where user_id=#{userRole.userId}")
//    int updateRole(@Param("userRole") UserRole userRole);
    
    
    //@Select("SELECT id,user_code userCode,user_name userName,user_tel userTel,CASE WHEN user_nature=1 THEN '直营' WHEN user_nature=1 THEN '加盟' ELSE '其他' END  userNature FROM user WHERE (user_code = #{qp.userCode}) AND (user_name = #{qp.userName}) AND (user_nature=#{qp.userNature}) AND (seal_mark = 0) LIMIT #{qp.start},#{qp.pageSize}")  
    @SelectProvider(type = UserAccountDaoProvider.class, method = "querySQL")
    List<UserAccountsVo> listUsersByPage(UserAccountListRequest userAccountListRequest);
    
    @SelectProvider(type = UserAccountDaoProvider.class, method = "queryTotalRecords")
    Integer queryUserTotalRecords(UserAccountListRequest userAccountListRequest);
    
    @Select("SELECT r.id id from account_role ar,role r where user_account_id=#{id} and ar.role_id=r.id")  
    List<Long> getSelectedRoleIds(@Param("id") Long id);
    
    @Select("SELECT r.id id,r.role_name name from account_role ar,role r where user_account_id=#{id} and ar.role_id=r.id and r.int001 is null ")
    List<SelectBoxDataVo> getRoleList(@Param("id") Long id);
    
    @Select("SELECT r.id id,r.role_name  name from role r where r.int001 is null and r.company_id=(select company_id from user_account where id=#{userId}) and r.enable_mark=1 and r.seal_mark = 0 and r.role_type=1")
    List<SelectBoxDataVo> getAllRoleList(@Param("userId") Long userId);
    
    
    @Select("SELECT id,user_name name from user where int001 is null and company_id=#{companyId} and seal_mark=0 order by create_date desc")
    List<SelectBoxDataVo> getAllUserList(@Param("companyId") Long companyId);
    
    @Select("SELECT id,user_name name from user where int001 is null and company_id=#{companyId} and seal_mark=0 and shop_id in (select distinct(shop_id) from shop_account where id in (select shop_account_id from user_acct_shop_acct where user_account_id = #{accountId})) order by create_date desc")
    List<SelectBoxDataVo> getCurrentUserList(@Param("companyId") Long companyId,@Param("accountId") Long accountId);
    
    @Update("UPDATE user_account SET account_pwd=#{pwd} WHERE id=#{id}")
    int updateUserAccountPwd(SaveAccountPwdRequest saveAccountPwdRequest);
    
    @Update("UPDATE user_account SET enable_mark=#{enableMark} WHERE id=#{id}")
    int updateAccountStartStop(SaveAccountStartStopRequest saveAccountStartStopRequest);
    
    @Update("UPDATE user_account SET account_status=#{accountStatus} WHERE id=#{id}")
    int setAccountStatusById(SetAccountStatusRequest setAccountStatus);
    
    @Update("UPDATE user_account SET account_status=#{accountStatus} WHERE account_name=#{accountName}")
    int setAccountStatusByName(SetAccountStatusRequest setAccountStatus);
    
    
    @Select("SELECT count(id) from user_account where account_name=#{accountName} and seal_mark=0")
    int getAccountCount(@Param("accountName") String accountName);
    
    @Select("SELECT count(id) from user_account where id=#{id} and int001 is not null ")  
    Integer getSuperManager(@Param("id") Long id);
    
    
}